#include <node.h>
#include <v8.h>
#include "ace/Log_Msg.h"
#include "ace/Reactor.h"
#include <arpa/inet.h>
#include "Reply_Handler.h"
#include "Announce_Handler.h"

using namespace v8;

ACE_THR_FUNC_RETURN event_loop (void *);

Handle<Value> Method(const Arguments& args) {
	//ACE_LOG_MSG->priority_mask (LM_INFO | LM_DEBUG, ACE_Log_Msg::PROCESS);

	char *hostname = "some-random-host.local";

	struct mdnsd *svr = mdnsd_start ();

	mdnsd_set_hostname (svr, hostname, inet_addr ("192.168.0.29"));

	const char *txt[] = {
		"srcvers=101.10",
		"feature=0x39f7",
		"deviceid=E0:CB:4E:AE:E0:31",
		NULL
	};

	struct mdns_service *svc = mdnsd_register_svc (svr, "Kyoungho",
	"_airplay._tcp.local", 8080, NULL, txt);

	mdns_service_destroy (svc);

	ACE_Reactor *reactor = ACE_Reactor::instance ();

	MDNS::Announce_Handler *announce_handler = new MDNS::Announce_Handler
		(reactor);
	announce_handler->open (svr);

	MDNS::Reply_Handler *reply_handler = new MDNS::Reply_Handler (reactor);
	reply_handler->open (svr);

	reactor->run_reactor_event_loop ();

  HandleScope scope;
  return scope.Close(String::New("HELLO"));
}

void init(Handle<Object> target) {
  target->Set(String::NewSymbol("hello"),
      FunctionTemplate::New(Method)->GetFunction());
}

NODE_MODULE(hello, init)
